using MT.Common;
using MT.Common.Helper;
using MT.Rest.Core.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Xunit;

namespace Mb.Rest.XUnitTest
{
    public class UnitTest1
    {
        private TestServer testServer;
        private HttpClient httpClient;
        private static readonly string userAgen = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36";

        public UnitTest1()
        {
            testServer = new TestServer(new WebHostBuilder().UseStartup<MT.Rest.Startup>());
            httpClient = testServer.CreateClient();
        }
        [Fact]
        public async void GetTest()
        {
            var data = await httpClient.GetAsync("api/test");
            var result = await data.Content.ReadAsStringAsync();
            Assert.Equal("300", result);
        }

        [Fact]
        public async void GetSessionKey()
        {
            var requst = new LoginRequest() {  LoginName="admin", Password="123456", AutoLogin=false, ClientTypeDic="pc"};
            httpClient.MaxResponseContentBufferSize = 256000;
            httpClient.DefaultRequestHeaders.Add("user-agent", userAgen);
            httpClient.CancelPendingRequests();
            httpClient.DefaultRequestHeaders.Clear();
            HttpContent httpContent = new StringContent(JsonHelper.ToJson(requst));
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var data = await httpClient.PostAsync("api/session/token", httpContent);
            var result = await data.Content.ReadAsStringAsync();
            Assert.NotNull(result);
        }
    }
}
